package com.missilike.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


/**
 * @author kin
 * @since 2017/11/30
 */
public class MyBatisPlusGenerator {

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(("请输入" + tip + "："));
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir("D:\\Code\\Java\\missilike\\back-end\\");
        gc.setFileOverride(true);
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columnList
        gc.setAuthor("kin");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("mtb");
        dsc.setPassword("1234=abcd");
        dsc.setUrl("jdbc:mysql://rm-bp1kcn0b019e7a2i4bo.mysql.rds.aliyuncs.com:3306/missilike?useUnicode=true&characterEncoding=UTF-8");
        mpg.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        //strategy.setTablePrefix(new String[] { "tlog_", "tsys_" });// 此处可以修改为您的表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setInclude(new String[]{"mil_content_statistic"}); // 需要生成的表
        //strategy.setExclude(new String[]{"test"}); // 排除生成的表
        strategy.setRestControllerStyle(true);
        //自动填充
//        List<TableFill> tableFillList = new ArrayList<>();
//        tableFillList.add(new TableFill("created", FieldFill.INSERT));
//        tableFillList.add(new TableFill("last_modified", FieldFill.INSERT_UPDATE));
//        strategy.setTableFillList(tableFillList);
        mpg.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.missilike");
        pc.setEntity("repository.entity");
        pc.setMapper("repository.mapper");
        pc.setXml("repository.mapping");
        pc.setService("biz");
        pc.setServiceImpl("biz.impl");
        pc.setController("admin");
        mpg.setPackageInfo(pc);

        mpg.execute();
    }

}
